package auctionhaus

import static org.junit.Assert.*

import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
 */
@TestFor(ListingController)
@Mock([Customer, Listing, ListingCreateService])
class ListingControllerTests {

    Customer seller
    
    void setUp() {
        seller = new Customer(email: "pkleczka@gmail.com", password: "1234567")
    }

    void testCreateValidListing()
    {
        Date endDate = new Date() + 2
        
        params.name = "ControllerTestListing"
        params.endDateTime = endDate
        params.startingBidPrice = 1.0f
        params.description = "Service test listing description."
        params.seller = seller

        controller.save()

        //validate we get sent to correct place
        assert "/listing/listMine/1" == response.redirectedUrl

        //validate we populate success message
        assert controller.flash.message != null
        assert controller.flash.message == "default.created.message"

        //validate listing is actually added
        assert Listing.count == 1
        Listing placedListing = Listing.findByName("ControllerTestListing")
        assert placedListing
        assert placedListing.endDateTime  ==  endDate
        assert placedListing.startingBidPrice == 1.0f
        assert placedListing.description == "Service test listing description."
        assert placedListing.seller == seller
    }

    void testCreateInvalidListing()
    {
        Date endDate = new Date() - 2

        params.name = "ControllerTestListing"
        params.endDateTime = endDate
        params.startingBidPrice = 1.0f
        params.description = "Service test listing description."
        params.seller = seller

        controller.save()

        //validate we get sent to correct place on error
        assert view == "/listing/create"

        //validate the error message
        assert ((Listing)(model.listingInstance)).errors['endDateTime'].code == "validator.invalid"

    }
}
